package com.wxh.bomb.netty.thread.queue;

import com.wxh.bomb.netty.thread.queue.bean.Netizen;

import java.util.concurrent.DelayQueue;

/**
 * 用网吧来实现延迟队列
 *
 * @author wxh
 * @date：2017.10.10
 */
public class Cafe implements Runnable {
    // 队列来装在网民
    private DelayQueue<Netizen> queue = new DelayQueue<Netizen>();

    /**
     * 上线
     *
     * @param netizen
     */
    private void online(Netizen netizen) {
        System.out.println("网名" + netizen.getName() + " 身份证" + netizen.getId() + "交钱" + netizen.getMoney() + "块,开始上机...");
        queue.add(netizen);
    }

    /**
     * 下线
     *
     * @param netizen
     */
    private void offline(Netizen netizen) {
        System.out.println("网名" + netizen.getName() + " 身份证" + netizen.getId() + "时间到下机...");
    }


    @Override
    public void run() {
        while (true) {
            try {
                Netizen netizen = queue.take();
                offline(netizen);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println("网吧开始营业");
        Cafe cafe = new Cafe();
        new Thread(cafe).start();
        for (int i = 1; i <= 20; i++) {
            cafe.online(new Netizen("路人" + i, "12" + i, i));
            Thread.sleep(1000);
        }
    }
}
